package top.roadvast.rabbitmq.provider;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import top.roadvast.rabbitmq.utils.RabbitmqUtil;

import java.io.IOException;
import java.util.Scanner;

/**
 * @Author 郭泽鹏
 * @Date 2021/12/27 15:18
 * @Version 1.0
 * @Description rabbitmq六种工作模式之一：主题模式
 */
public class TopicProvider {
    public static void main(String[] args) throws IOException {
        final Connection connection = RabbitmqUtil.getConnection();
        final Channel channel = connection.createChannel();
        //交换机名称，交换机类型：主题交换机
        channel.exchangeDeclare("topic_logs", BuiltinExchangeType.TOPIC);
        while (true) {
            System.out.println("输入消息：");
            final String message = new Scanner(System.in).nextLine();
            if ("exit".equalsIgnoreCase(message.trim())) {
                break;
            }
            System.out.println("输入键值：");
            //路由键,这里我们用日志级别,如"error","info","warning"，以及操作用户，如"lucy","roadvast","jenny"
            //例如：[error.lucy],[info.roadvast]
            final String routingKey = new Scanner(System.in).nextLine();
            channel.basicPublish("topic_logs", routingKey, null, message.getBytes());
            System.out.println("消息已发送.rountingKey=" + routingKey + ", message=" + message);
        }
        RabbitmqUtil.closeConnection(connection, channel);
    }
}
